package com.heima.kafka.controller;

import cn.hutool.json.JSONUtil;
import com.heima.KafkaApplication;
import com.heima.kafka.dto.UserDto;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class HelloController {
    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;
    @GetMapping("/hello")
    public String hello(@RequestParam(value = "msg", defaultValue = "哈哈") String msg){
        kafkaTemplate.send("itcast-topic", msg);
        return "消息发送成功, msg = " + msg;
    }

    @PostMapping("/user")
    public String user(@RequestBody UserDto userDto){
        String jsonStr = JSONUtil.toJsonStr(userDto);
        kafkaTemplate.send("itcast-topic-user", jsonStr);
        return "消息发送成功, jsonStr = " + jsonStr;
    }
}
